package www.bancodelanacion.intranet.prestamos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import www.bancodelanacion.intranet.prestamos.entidad.ClienteDTO;
import www.bancodelanacion.intranet.prestamos.entidad.RefinanciamientoDTO;
import www.bancodelanacion.intranet.prestamos.util.MySqlDBConn;


public  class ClienteDAOImpl implements ClienteDAO {
//implementa
	@Override
	public int insertaCliente(ClienteDTO bean) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {


				conn = new MySqlDBConn().getConnection();
				String sql ="insert into tb_cliente values(?,null,?,?,?,?,?,?,2)";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1,bean.getCodigocliente());
				pstm.setString(2,bean.getRazonsocial());
				pstm.setString(3,bean.getDireccion());
				pstm.setString(4,bean.getRuc());
				pstm.setString(5,bean.getFax());
				pstm.setString(6,bean.getPaginaweb());
				pstm.setString(7,bean.getCorreo());
				return pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return -1;
	}

	@Override
	public int actualizaCliente(ClienteDTO bean) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="update tb_cliente set  Razon_Social=?, " +
						"Direccion=?,Ruc=?,Fax=?,Pagina_Web=?,Correo=? where Codigo_Cliente=?";
				pstm = conn.prepareStatement(sql);
				
				pstm.setString(1,bean.getRazonsocial());
				pstm.setString(2,bean.getDireccion());
				pstm.setString(3,bean.getRuc());
				pstm.setString(4,bean.getFax());
				pstm.setString(5,bean.getPaginaweb());
				pstm.setString(6,bean.getCorreo());
				pstm.setString(7,bean.getCodigocliente());
				return pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return -1;
	}

	public 	int eliminaCliente(String codigo) throws SQLException {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="update tb_cliente set  desafiliado=? where Codigo_Cliente=?";
				pstm = conn.prepareStatement(sql);
				pstm.setInt(1,1);
				pstm.setString(2,codigo);
				return pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return -1;
	}

	@Override
	public ArrayList<ClienteDTO> listarCliente()throws SQLException  {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ArrayList<ClienteDTO> data = new ArrayList<ClienteDTO>();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_cliente where desafiliado=2 Order by id_cliente";
				pstm = conn.prepareStatement(sql);
				rs = pstm.executeQuery();
				ClienteDTO dto = null;
				while(rs.next()){
					dto = new ClienteDTO();
					
					dto.setCodigocliente(rs.getString("Codigo_Cliente"));
					dto.setRazonsocial(rs.getString("Razon_Social"));
					dto.setDireccion(rs.getString("Direccion"));
					dto.setRuc(rs.getString("Ruc"));
					dto.setFax(rs.getString("Fax"));
					dto.setPaginaweb(rs.getString("Pagina_Web"));
					dto.setCorreo(rs.getString("Correo"));
					dto.setDesafiliado("Afiliado");
					
					data.add(dto);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return data;
	}
	
	public String codigogen(){
		
		
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
	
		int i=0;
		String cadena="";
		int n=0;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select count(id_cliente) as cantidad   from tb_cliente ";
				pstm = conn.prepareStatement(sql);
				rs = pstm.executeQuery();
				
				while(rs.next()){
					i=rs.getInt("cantidad");
					System.out.println(i);
					if(i==0){
						System.out.println("primero");
						i=1;
						cadena="CLI0"+i;
						return cadena;
						
					}else{
						System.out.println("siguiente"+i);
						
						i++;
						
						cadena="CLI0"+i;
						return cadena;
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return cadena;
	}

	@Override
	public ClienteDTO buscaCliente(String codigo_cliente) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ClienteDTO dto = null;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_cliente where Codigo_Cliente=?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, codigo_cliente);
				rs = pstm.executeQuery();
				dto = new ClienteDTO();
				if(rs.next()){
					dto.setCodigocliente(rs.getString("Codigo_Cliente"));
					dto.setRazonsocial(rs.getString("Razon_Social"));
					dto.setDireccion(rs.getString("Direccion"));
					dto.setRuc(rs.getString("Ruc"));					
					dto.setFax(rs.getString("Fax"));
					dto.setPaginaweb(rs.getString("Pagina_Web"));
					dto.setCorreo(rs.getString("Correo"));

				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return dto;
	}

	@Override
	public boolean buscaDuplicado(String razon, String ruc)
			throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
	
		boolean flag=false;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select Razon_Social,Ruc from tb_cliente where Razon_Social=? or Ruc=?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, razon);
				pstm.setString(2, ruc);
				rs = pstm.executeQuery();
			
			
				if(rs.next()){
				
				
					
					flag=true;
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}

		return flag;
	}

	@Override
	public ArrayList<ClienteDTO> buscarCliente(String codigo,
			String nombre, String ruc) throws SQLException {
		
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ArrayList<ClienteDTO> data = new ArrayList<ClienteDTO>();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_cliente where Codigo_Cliente like ? or Razon_Social like ? or Ruc like ?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, codigo);
				pstm.setString(2, nombre);
				pstm.setString(3, ruc);
				rs = pstm.executeQuery();
				ClienteDTO r = null;
				while(rs.next()){
					r = new ClienteDTO();
					r.setCodigocliente(rs.getString("Codigo_Cliente"));
					r.setRazonsocial(rs.getString("Razon_Social"));
					r.setDireccion(rs.getString("Direccion"));
					r.setRuc(rs.getString("Ruc"));					
					r.setFax(rs.getString("Fax"));
					r.setPaginaweb(rs.getString("Pagina_Web"));
					r.setCorreo(rs.getString("Correo"));
					data.add(r);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return data;
	}

	@Override
	public ClienteDTO obtenerClienteDAO(String codigo) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ClienteDTO  r = new ClienteDTO();
		try {
			conn = new MySqlDBConn().getConnection();
			String sql ="select * from tb_cliente where Codigo_Cliente like ?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, codigo);
			rs = pstm.executeQuery();
			if(rs.next()){
				r.setCodigocliente(rs.getString("Codigo_Cliente"));
				r.setRazonsocial(rs.getString("Razon_Social"));
				r.setDireccion(rs.getString("Direccion"));
				r.setRuc(rs.getString("Ruc"));					
				r.setFax(rs.getString("Fax"));
				r.setPaginaweb(rs.getString("Pagina_Web"));
				r.setCorreo(rs.getString("Correo"));
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return r;
	}

	

	}
	
	


